package cn.yangliu.nacos.comm.ex;

import java.util.Objects;

import lombok.Getter;
import lombok.Setter;

/**
 * The type Global exception.
 *
 * <p>
 * .
 *
 * @author 问道于盲
 * @date 2019 -12-14
 */
@Getter
@Setter
public class GlobalException extends RuntimeException {

    /**
     * current class instance's member.
     * The Code.
     */
    private int code;

    /**
     * current class instance's member.
     * The Msg.
     */
    private String msg;

    /**
     * current class instance's member.
     * The Result code.
     */
    private ResultCode resultCode;

    /**
     * Instantiates a new Global exception.
     * allow any class to create a new instance.
     *
     * @param resultCode the result code
     */
    public GlobalException(ResultCode resultCode) {
        this(resultCode.getCode(), resultCode.getMsg());
        this.resultCode = resultCode;
    }

    /**
     * Instantiates a new Global exception.
     * allow any class to create a new instance.
     *
     * @param resultCode the result code
     * @param append     the append
     */
    public GlobalException(ResultCode resultCode, String append) {
        String msg = resultCode.getMsg();
        if (!Objects.equals(msg, append)){
            msg = "," + append;
        }
        this.msg = msg;
        this.code = resultCode.getCode();
    }

    /**
     * Instantiates a new Global exception.
     * allow any class to create a new instance.
     *
     * @param resultCode the result code
     * @param throwable  the throwable
     */
    public GlobalException(ResultCode resultCode, Throwable throwable) {
        this(resultCode.getCode(), resultCode.getMsg(), throwable);
        this.resultCode = resultCode;
    }

    /**
     * Instantiates a new Global exception.
     * allow any class to create a new instance.
     *
     * @param code the code
     * @param msg  the msg
     */
    public GlobalException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * Instantiates a new Global exception.
     * allow any class to create a new instance.
     *
     * @param code  the code
     * @param msg   the msg
     * @param cause the cause
     */
    public GlobalException(int code, String msg, Throwable cause) {
        super(cause);
        this.code = code;
        this.msg = msg;
    }

    /**
     * current class method for getter.
     * Gets message.
     *
     * @return the message
     */
    @Override
    public String getMessage() {
        return msg;
    }
}
